package com.ywqf_api.service.impl;

import com.ywqf_api.base.BaseException;
import com.ywqf_api.common.enums.FilePaths;
import com.ywqf_api.dao.MaintenanceDao;
import com.ywqf_api.dto.in.MaintenanceDto;
import com.ywqf_api.entity.Customer;
import com.ywqf_api.entity.MainStaff;
import com.ywqf_api.entity.Maintenance;
import com.ywqf_api.service.MaintenanceService;
import com.ywqf_api.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by Administrator on 2018/4/3 0003.
 */
@Service
public class MaintenanceServiceImpl implements MaintenanceService {
    @Autowired
    private MaintenanceDao maintenanceDao;
    @Override
    public Map findMaintenance(Integer houseId, Integer customerId) {
        Integer count =  maintenanceDao.findMaintenance(houseId,customerId);
        if(count == null){
            count = 0;
        }
        Customer customer = maintenanceDao.findCustomer(customerId);
        if(customer == null ){
            return null;
        }
        Map map = new HashMap();
        map.put("count",count);
        map.put("name",customer.getName());
        map.put("phone",customer.getTelephone());
        return  map;
    }

    @Override
    public String file(MultipartFile file) {
        String url = UploadUtil.saveFile(file, FilePaths.MAINTENANCE);
        return url;
    }

    @Override
    @Transactional
    public int saveMaintenance(MaintenanceDto maintenanceDto) {
        Random rand=new Random();//生成随机数
        String cardNnumer="";
        for(int a=0;a<12;a++){
            cardNnumer+=rand.nextInt(10);//生成12位数字
        }
        maintenanceDto.setMaintenanceNum(cardNnumer);
        int save = maintenanceDao.saveMaintenance(maintenanceDto);
        if(save > 0){
            Integer maintenanceId = maintenanceDto.getMaintenanceId();
            String[] a = maintenanceDto.getUrl().split(",");
            for(int i=0; i<a.length;i++){
                String url = a[i];
                int add =maintenanceDao.saveUrl(url,maintenanceId);
                if(add < 1){
                    throw new BaseException("添加失败");
                }
            }
            return 1;
        }else{
            throw new BaseException("添加失败");
        }
    }

    @Override
    @Transactional
    public int saveUrlAndMaintenance(MaintenanceDto maintenanceDto) {
        Random rand=new Random();//生成随机数
        String cardNnumer="";
        for(int a=0;a<12;a++){
            cardNnumer+=rand.nextInt(10);//生成12位数字
        }
        maintenanceDto.setMaintenanceNum(cardNnumer);
        int save = maintenanceDao.saveMaintenance(maintenanceDto);
        if(save > 0){
            Integer maintenanceId = maintenanceDto.getMaintenanceId();
            for(int o=0; o<maintenanceDto.getFile().size(); o++){
                MultipartFile file = maintenanceDto.getFile().get(o);
                String url = UploadUtil.saveFile(file, FilePaths.USER);
                url = "developer/"+url;
                int add =maintenanceDao.saveUrl(url,maintenanceId);
                if(add < 1){
                    throw new BaseException("添加失败");
                }
            }
            return 1;
        }else{
            throw new BaseException("添加失败");
        }
    }

    @Override
    public List<Maintenance> findMainDetail(Integer houseId) {
        List<Maintenance> list = maintenanceDao.findMainDetail(houseId);
        if(list.size() > 0){
            for (int i =0; i<list.size();i++){
                Maintenance maintenance = list.get(i);
                if(maintenance.getReportRepaireTime().length()>=19){
                    maintenance.setReportRepaireTime(maintenance.getReportRepaireTime().substring(0,19));
                }
                if(maintenance.getEndTime()!=null){
                    if(maintenance.getEndTime().length()>=19){
                        maintenance.setEndTime(maintenance.getEndTime().substring(0,19));
                    }
                }
              if(maintenance.getCheckTime() != null){
                    if(maintenance.getCheckTime().length()>=19){
                        maintenance.setCheckTime(maintenance.getCheckTime().substring(0,19));
                    }
              }
              if(maintenance.getOperateTime().length()>= 19){
                  maintenance.setOperateTime(maintenance.getOperateTime().substring(0,19));
              }
            }
            return list;
        }else{
            return null;
        }
    }

    @Override
    public Map findMainIdDetail(Integer maintenanceId) {
        Maintenance maintenance = maintenanceDao.findDetail(maintenanceId);
        if(maintenance == null){
            return null;
        }else{
            MainStaff staffId = maintenanceDao.findStaffId(maintenanceId);
            if(staffId != null){
                if(staffId.getSendTime().length()>=19){
                    maintenance.setSendTime(staffId.getSendTime().substring(0,19));
                }
               if(maintenance.getReportRepaireTime().length()>=19){
                   maintenance.setReportRepaireTime(maintenance.getReportRepaireTime().substring(0,19));
               }
                if(maintenance.getEndTime() != null){
                   if(maintenance.getEndTime().length()>=19){
                       maintenance.setEndTime(maintenance.getEndTime().substring(0,19));
                   }
                }
                if(maintenance.getCheckTime() != null){
                    if(maintenance.getCheckTime().length()>=19){
                        maintenance.setCheckTime(maintenance.getCheckTime().substring(0,19));
                    }
                }
                if(maintenance.getOperateTime().length()>=19){
                    maintenance.setOperateTime(maintenance.getOperateTime().substring(0,19));
                }
                Maintenance data = maintenanceDao.findStaffDetail(staffId.getStaffId());
                maintenance.setName(data.getName());
                maintenance.setPhone(data.getPhone());
            }
            List<Maintenance> url = maintenanceDao.findUrl(maintenanceId);
            Map map = new HashMap();
            if(url.size() > 0){
                map.put("url",url);
                map.put("detail",maintenance);
                return map;
            }else{
                map.put("url",url);
                map.put("detail",null);
                return map;
            }
        }
    }
}
